﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KCMS.BusinessLogic.Interfaces;
using StructureMap;
using KCMS.BusinessLogic.Models;
using System.IO;

namespace KCMS.Areas.Admin.Engine
{
    public class ImageUploader2
    {
        private String directory = AppDomain.CurrentDomain.BaseDirectory + "Content/UserImages/upload/";
        private IProductService pService = ObjectFactory.GetInstance<IProductService>();
        private IProductCategoryService pCatService = ObjectFactory.GetInstance<IProductCategoryService>();

        public String SaveImage(HttpPostedFileBase imageFile, ProductImage productImage,String metka=null)
        {
            if (imageFile != null)
            {
                           

                Product product = pService.getById(productImage.ProductId);
               
                String effPath = product.Article+"-"+productImage.Color+"-";
                if (metka != null)
                {
                    effPath = effPath + metka+"-";
                }
                effPath=effPath+imageFile.FileName;
                //if (!Directory.Exists(directory + effPath))
                //    Directory.CreateDirectory(directory + effPath);
                //effPath += imageFile.FileName;
                imageFile.SaveAs(directory + effPath);
                return effPath;
            }
            else return null;
        }
        public void Delete(String effPath)
        {
            if (effPath != null)
            {
                System.IO.File.Delete(directory + effPath);
            }
        }
        public String UpdateImage(HttpPostedFileBase newImageFile, String effPath,ProductImage productImage,String metka=null)
        {
            Delete(effPath);
            return SaveImage(newImageFile, productImage,metka);
        }
    }
}